package com.whut.cristiano.autographbook.Fragment;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.whut.cristiano.autographbook.Model.HandlePictureListener;
import com.whut.cristiano.autographbook.Model.PhoneDialog;
import com.whut.cristiano.autographbook.R;
import com.whut.cristiano.autographbook.Util.DrawImageUtil;
import com.whut.cristiano.autographbook.Util.ImageUtil;
import com.whut.cristiano.autographbook.Util.SharedPrefUtil;
import com.whut.cristiano.autographbook.Util.ToastUtil;

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link TemplateThreeFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link TemplateThreeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class TemplateThreeFragment extends Fragment implements View.OnClickListener {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private ImageView imageView;
    private Bitmap imageBitmap;
    private Bitmap textBitmap;
    private TextWatcher textWatcher;
    private EditText editText;
    private ProgressDialog progressDialog;
    private Activity activity;
    private Button saveDrawBtn;
    private Button submitBtn;

    private OnFragmentInteractionListener mListener;

    public TemplateThreeFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @return A new instance of fragment TemplateThreeFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static TemplateThreeFragment newInstance() {
        TemplateThreeFragment fragment = new TemplateThreeFragment();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        activity = getActivity();
        View view = inflater.inflate(R.layout.fragment_template_three, container, false);
        initWatcher();
        saveDrawBtn = (Button) view.findViewById(R.id.save_draw);
        saveDrawBtn.setOnClickListener(this);
        submitBtn = (Button) view.findViewById(R.id.submit_autographbook_three);
        submitBtn.setOnClickListener(this);
        progressDialog = new ProgressDialog(activity);
        editText = (EditText) view.findViewById(R.id.template_three_edit);
        editText.addTextChangedListener(textWatcher);
        imageView = (ImageView) view.findViewById(R.id.template_three_image);
        imageBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.template_three);
        imageView.setImageBitmap(imageBitmap);
        return view;
    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    private void initWatcher(){
        textWatcher = new TextWatcher() {
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }
            public void beforeTextChanged(CharSequence s, int start, int count,int after) {}
            public void afterTextChanged(Editable s) {
                textBitmap = DrawImageUtil.drawTextToBitmapTemplateTwo(getActivity(),imageBitmap,
                        s.toString(),0,0, ContextCompat.getColor(getContext(),R.color.black_translucent),"three.TTF");
                imageView.setImageBitmap(textBitmap);
            }
        };
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.save_draw:
                ImageUtil imageUtil = new ImageUtil(imageView,activity);
                if (textBitmap==null){
                    textBitmap = imageBitmap;
                }
                imageUtil.templateSavePhoto(textBitmap, new HandlePictureListener() {
                    @Override
                    public void onStart() {}

                    @Override
                    public void onStop(String picUrl, Boolean isUploaded, String time) {
                        SharedPrefUtil.saveTemplateOnePicUrl(picUrl);
                        ToastUtil.showToast(activity,"保存成功");
                    }

                    @Override
                    public void onError(Exception e) {}
                });
                break;
            case R.id.submit_autographbook_three:
                PhoneDialog dialog = new PhoneDialog();
                dialog.show(getChildFragmentManager(), "phoneDialog");
                break;
        }
    }

//    @Override
//    public void onAttach(Context context) {
//        super.onAttach(context);
//        if (context instanceof OnFragmentInteractionListener) {
//            mListener = (OnFragmentInteractionListener) context;
//        } else {
//            throw new RuntimeException(context.toString()
//                    + " must implement OnFragmentInteractionListener");
//        }
//    }
//
//    @Override
//    public void onDetach() {
//        super.onDetach();
//        mListener = null;
//    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }
}
